package com.tinusgraglin.enclusiv.ui.components

import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.tinusgraglin.enclusiv.R
import com.tinusgraglin.enclusiv.ui.theme.DisableGrey

@Composable
fun FantasticCircularButtonWithLoadingIndicator(
    textStr: String,
    onClick: () -> Unit,
    isLoading: Boolean,
    enabled: Boolean,
    textSize: TextUnit = 22.sp,
    textFontWeight: FontWeight = FontWeight.Normal,
    textFontStyle: FontStyle = FontStyle.Italic,
    size: Dp = 80.dp,
    loadingIndicatorSize: Dp = 32.dp,
) {
    val loadingIconInfTrans = rememberInfiniteTransition()
    val loadingIconRotateAngle by loadingIconInfTrans.animateFloat(
        initialValue = 0.0F,
        targetValue = 360.0F,
        animationSpec = infiniteRepeatable(
            animation = tween(1000),
        )
    )

    Button(
        onClick = onClick,
        enabled = enabled,
        modifier = Modifier.size(size),
        shape = CircleShape,
        contentPadding = PaddingValues(),
        colors = ButtonDefaults.buttonColors(disabledContentColor = DisableGrey)
    ) {
        Box(contentAlignment = Alignment.Center) {
            Image(painter = painterResource(id = R.drawable.go),
                modifier = Modifier.fillMaxSize(), contentDescription = "Button: $textStr.")
            if (isLoading) {
                Image(painter = painterResource(id = R.drawable.loader),
                    modifier = Modifier
                        .size(loadingIndicatorSize)
                        .rotate(loadingIconRotateAngle),
                    contentDescription = "Button: $textStr, is loading.")
            } else {
                Text(
                    text = textStr, textAlign = TextAlign.Center, fontSize = textSize,
                    fontWeight = textFontWeight, fontStyle = textFontStyle,
                )
            }
        }
    }
}